package com.rzx.mybackend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzx.mybackend.common.PageResult;
import com.rzx.mybackend.dto.BuyMerchandiseDTO;
import com.rzx.mybackend.dto.MerchandisePageDTO;
import com.rzx.mybackend.entity.Merchandise;
import com.rzx.mybackend.mapper.MerchandiseMapper;
import com.rzx.mybackend.service.MerchandiseService;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * MerchandiseServiceImpl
 *
 * @author renzhexian
 * @date 2025/8/13
 */
@Service
@RequiredArgsConstructor
public class MerchandiseServiceImpl extends ServiceImpl<MerchandiseMapper,Merchandise> implements MerchandiseService {

    private final MerchandiseMapper merchandiseMapper;

    /**
     * 获取查询条件
     *
     * @param merchandisePageDTO
     * @return
     */
    private static LambdaQueryWrapper<Merchandise> getMerchandiseLambdaQueryWrapper(MerchandisePageDTO merchandisePageDTO) {
        LambdaQueryWrapper<Merchandise> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(StringUtil.isNotBlank(merchandisePageDTO.getMerchandiseName()),Merchandise::getMerchandiseName, merchandisePageDTO.getMerchandiseName());
        return wrapper;
    }

    /**
     * 分页查询
     *
     * @param merchandisePageDTO
     * @return
     */
    @Override
    public PageResult<Merchandise> page(MerchandisePageDTO merchandisePageDTO) {
        PageHelper.startPage(merchandisePageDTO.getPage(), merchandisePageDTO.getPageSize());
        Page<Merchandise> merchandisePage = merchandiseMapper.query(merchandisePageDTO);
        return new PageResult<>(merchandisePage.getTotal(), merchandisePage.getResult());
    }

    /**
     * 扣减库存
     *
     * @param merchandiseId
     * @param totalQuantity
     */
    @Override
    public void releaseStock(Long merchandiseId, int totalQuantity) {
        merchandiseMapper.releaseStock(merchandiseId,totalQuantity);
    }

    /**
     * 扣减库存
     * @param merchandiseId
     * @param integer
     */
    @Override
    public void refundStock(Long merchandiseId, Integer integer) {
        // todo: 回退库存逻辑；
    }

}
